package com.fanhao.dllo.justforyou.activity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.activity.HomeActivity;
import com.fanhao.dllo.justforyou.entity.greendao.PersonEntity;
import com.fanhao.dllo.justforyou.helper.PersonHelper;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView imgView;
    private EditText nameEt, pswEt;
    private Button loginBtn, registerBtn;
    private RadioButton yesRb,noRb;

   //  登录成功后的消息提醒
    private NotificationManager manager;

    //helper 帮助类
    private PersonHelper helper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
    }

    public void initView() {
        imgView = (ImageView) findViewById(R.id.imgIv);
        nameEt = (EditText) findViewById(R.id.nameEt);
        pswEt = (EditText) findViewById(R.id.pswEt);
        loginBtn = (Button) findViewById(R.id.loginBtn);
        registerBtn = (Button) findViewById(R.id.registerBtn);
        yesRb  = (RadioButton)findViewById(R.id.yesRb);
        noRb = (RadioButton)findViewById(R.id.noRb);

        loginBtn.setOnClickListener(this);
        registerBtn.setOnClickListener(this);
        helper = new PersonHelper().getInstance();
        yesRb.setOnClickListener(this);
        noRb.setOnClickListener(this);

        manager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);

    }

    public void sendNotification(View v) {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setTicker("恭喜你登陆成功~");
        builder.setSmallIcon(R.mipmap.justforyou);

        Bitmap largeIcon = BitmapFactory.decodeResource(
                getResources(),R.mipmap.justforyou);
        builder.setLargeIcon(largeIcon);

        builder.setContentTitle("恭喜你登录成功~");
        builder.setContentText("你已成功登录 JUST FOR YOU ~");
//        builder.setContentInfo("chouchou , 你来啦~嘎嘎");
        builder.setWhen(System.currentTimeMillis());
        builder.setAutoCancel(true);

        Intent intent = new Intent(this,NotificationActivity.class);
        // 延时跳转
        PendingIntent pi = PendingIntent.getActivity(this,700,intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setContentIntent(pi);
        manager.notify(100,builder.getNotification());

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {


            case R.id.loginBtn:
               String name = nameEt.getText().toString();
                String psw = pswEt.getText().toString();


                //判断内容是否为空
                if ( !name.equals("") && !psw.equals("")) {
                    PersonEntity person = helper.getPersonEntityByName(name);
                    if (person != null) {


//                 if (flagName == true) {
                        // 判断是否有该用户名
                        if (name.equals(person.getName())) {
                            //
                            if (psw.equals(person.getPsw())) {
                                if (yesRb.isChecked()) {


                                Intent loginLoveIntent = new Intent(this, HomeActivity.class);
                                    loginLoveIntent.putExtra("name",name);

                                startActivity(loginLoveIntent);

                                overridePendingTransition(
                                        R.anim.right_into, R.anim.left_out);
                                    sendNotification(v);

                                finish();
                                } else {
                                    Intent loginByeIntent = new Intent(this, GoodByeActivtiy.class);
                                    startActivity(loginByeIntent);
                                    overridePendingTransition(
                                            R.anim.right_into, R.anim.left_out);
                                    finish();
                                }

                            } else {
                                Toast.makeText(this, "密码不正确!",
                                        Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(this, "用户名不正确!",
                                    Toast.LENGTH_SHORT).show();
                        }
                    } else {Toast.makeText(this, "用户不存在!",
                            Toast.LENGTH_SHORT).show();}
                }else{
                    Toast.makeText(this, "用户名或密码不能为空!",
                            Toast.LENGTH_SHORT).show();
                }


                break;

            case R.id.registerBtn:
                Intent registerIntent = new Intent(this, RegisterActivity.class);
                startActivity(registerIntent);

                overridePendingTransition(
                        R.anim.right_into, R.anim.left_out);

                break;

        }
    }
}

